#region Copyright and Licensing Notice
// Copyright (C) 2011 Steven Bosnick
// 
// This program is open source software; you can redistribute it 
// and/or modifiy it under the terms of the Eclipse Public License,
// version 1.0.
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A 
// PARTICULAR PURPOSE. See the Eclipse Public License for more
// details.
// 
// You should have received a copy of the Eclipse Public License 
// along with this program; if not, you can find a copy at
// "http://www.opensource.org/licenses/eclipse-1.0.php".
#endregion
using System;
using BitwiseOre.SharedMemory;
using NUnit.Framework;

namespace BitwiseOre.Test.SharedMemory.Integration
{
    [TestFixture]
    public sealed class ShardMemoryContextFixture : AssertionHelper
    {
        private SharedMemoryContext _sut;

        [SetUp]
        public void SetUp()
        {
            _sut = new SharedMemoryContext();
        }

        [Test(Description = "The listener context holds its values between different times retreiving them.")]
        public void ListenerContext_HoldsItsValues()
        {
            const int maxpending = 77;

            _sut.ListenerContext.MaxPendingConnections = maxpending;

            Expect(_sut.ListenerContext.MaxPendingConnections, EqualTo(maxpending),
                   "Unexpected value contained in the listener context.");
        }

        [Sequential,Test(Description = "The maxium pending connections property must be positive or too large.")]
        public void MaxPendingConnections_RejectsInvalidValues(
            [Values(0, -1, -5, 0xffe, 0xfff)]int pendingConnections)
        {
            Expect(() => _sut.ListenerContext.MaxPendingConnections = pendingConnections,
                   Throws.InstanceOf<ArgumentOutOfRangeException>());
        }

        [Test(Description = "The stream context holds its valudes between different time retreiving them.")]
        public void StreamContext_HoldsItsValues()
        {
            const int chunckingfactor = 10;
            const int buffersize = 100;

            _sut.StreamContext.BufferSize = buffersize;
            _sut.StreamContext.ChunckingFactor = chunckingfactor;

            Expect(_sut.StreamContext.BufferSize, EqualTo(buffersize),
                   "Unexpected value contained in the stream context.");
            Expect(_sut.StreamContext.ChunckingFactor, EqualTo(chunckingfactor),
                   "Unexpected value contained in the stream context.");
        }

        [Sequential, Test(Description = "The buffer size must be positive.")]
        public void BufferSize_RejectsInvalidValues(
            [Values(0, -1, -5, int.MaxValue >> 2)] int bufferSize)
        {
            Expect(() => _sut.StreamContext.BufferSize = bufferSize, 
                Throws.InstanceOf<ArgumentOutOfRangeException>());
        }

        [Sequential, Test(Description = "The chunking factor must be positive.")]
        public void ChunkingFactor_RejectsInvalidValdues(
            [Values(0, -1, -5)] int chunkingFactory)
        {
            Expect(() => _sut.StreamContext.ChunckingFactor = chunkingFactory,
                   Throws.InstanceOf<ArgumentOutOfRangeException>());
        }

        [Sequential, Test(Description = "The buffer size must not be smaller then the current chunking factor.")]
        public void BufferSize_SmallerThanChunkingFactor(
            [Values(5, 5, 7, 2047, 2048)] int buffferSize,
            [Values(6, 7, 13, 2048, 2049)] int chunkingFactor)
        {
            _sut.StreamContext.BufferSize = 3000;
            _sut.StreamContext.ChunckingFactor = chunkingFactor;
            Expect(() => _sut.StreamContext.BufferSize = buffferSize,
                   Throws.InstanceOf<ArgumentException>());
        }

        [Sequential, Test(Description = "The chunking factor must not be larger than the current buffer size.")]
        public void ChunkingFactor_LargerThanBufferSize(
            [Values(5, 5, 7, 2047, 2048)] int buffferSize,
            [Values(6, 7, 13, 2048, 2049)] int chunkingFactor)
        {
            _sut.StreamContext.BufferSize = buffferSize;
            Expect(() => _sut.StreamContext.ChunckingFactor = chunkingFactor,
                   Throws.InstanceOf<ArgumentException>());
        }
    }

}